package com.portal.tag;

import java.util.Map;
import java.util.Set;

import javax.portlet.PortletConfig;
import javax.portlet.PortletRequest;
import javax.servlet.jsp.JspException;

import com.portal.util.constants.TagConstants;

public abstract class PortletURLTag286 extends PortletURLTag168 {

  protected Boolean copyCurrentRenderParameters = false;

  public PortletURLTag286() {
    super();
    this.escapeXml = true;
  }

  @Override
  public int doStartTag() throws JspException {

    PortletConfig portletConfig = (PortletConfig) pageContext.getRequest().getAttribute(TagConstants.PORTLET_CONFIG);
    Map<String, String[]> containerRuntimeOptions = portletConfig.getContainerRuntimeOptions();
    if (containerRuntimeOptions != null) {
      String[] result = containerRuntimeOptions.get(TagConstants.ESCAPE_XML_RUNTIME_OPTION);
      if (result != null) {
        if (result.length > 0) {
          if (result[0].equals(true))
            escapeXml = true;
          else if (result[0].equals(false))
            escapeXml = false;
        }
      }
    }

    return super.doStartTag();
  }

  @Override
  public int doEndTag() throws JspException {

    if (copyCurrentRenderParameters) {
      /* prepend current render parameters */
      doCopyCurrentRenderParameters();
    }

    return super.doEndTag();
  }

  public Boolean getCopyCurrentRenderParameters() {
    return copyCurrentRenderParameters;
  }

  public void setCopyCurrentRenderParameters(Boolean copyCurrentRenderParameters) {
    this.copyCurrentRenderParameters = copyCurrentRenderParameters;
  }

  protected void doCopyCurrentRenderParameters() {
    PortletRequest request = (PortletRequest) pageContext.getRequest().getAttribute(TagConstants.PORTLET_REQUEST);

    if (request != null) {
      Map<String, String[]> renderParamsMap = request.getPrivateParameterMap();
      Set<String> keySet = renderParamsMap.keySet();

      for (String key : keySet) {
        if (!removedParametersList.contains(key)) {
          String[] values = renderParamsMap.get(key);
          for (int index = 0; index < values.length; ++index) {
            addParameter(key, values[index]);
          }
        }
      }
    }
  }

}
